// pages/personal/personal.js
const app = getApp()
var util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nickname: '',
    attrImg: '',
    is_sex:0,
    sexs: [
      {
        value: 0,
        name: '女'
      },
      {
        value: 1,
        name: '男'
      }
    ],
    birthday: '',
    headList: ['/images/girl.png','/images/boy.png'],
    isSelect: false
  },
  showSelect(){
    this.setData({
      isSelect: true
    })
  },
  onSelect(e){
    console.log(e.currentTarget.dataset.url)
    this.setData({
      attrImg:e.currentTarget.dataset.url,
      isSelect: false
    })
  },
  radioChange(e) {
    let sexs = this.data.sexs;
    console.log(e)
    var is_sex;
    for (let i = 0, len = sexs.length; i < len; ++i) {
      sexs[i].checked = sexs[i].value === e.detail.value
      
    }

    this.setData({
     is_sex: e.detail.value
    })
  },
  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      birthday: e.detail.value
    })
  },onConfirm(){
if(this.data.is_sex ==0){
  var gender='女';
}else{
  var gender='男';
}
    app.curl(util.web_url.personinfor,{token:wx.getStorageSync('token'),'avatar':this.data.attrImg,birth:this.data.birthday,gender:gender},'POST',false,false,this,getCurrentPages()).then( (res)=>{
       wx.setStorageSync('userinfo.avatar', this.data.attrImg)
       wx.setStorageSync('userinfo.sex', this.data.is_sex)
       wx.setStorageSync('userinfo.birth', this.data.birthday)
      
      wx.navigateBack({
        delta: 1
      });
     
      console.log(res.data.length)
      var headList = [];
      for(var i=0;i<res.data.length;i++){
        //res.data[i].url = app.globalData.domain+res.data[i].url
        headList[i] = app.globalData.domain+res.data[i].url
      }
       console.log(headList)
       that.setData({headList: headList})
      

  }).catch( (errMsg)=>{});
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this
      app.curl(util.web_url.get_headimg,that.data.form_data,'POST',false,false,this,getCurrentPages()).then( (res)=>{

    
     
      
      console.log(res.data.length)
      var headList = [];
      for(var i=0;i<res.data.length;i++){
        //res.data[i].url = app.globalData.domain+res.data[i].url
        headList[i] = app.globalData.domain+res.data[i].url
      }
       console.log(headList)
       that.setData({headList: headList})

       app.curl(util.web_url.my,{token:wx.getStorageSync('token')},'POST',false,false,this,getCurrentPages()).then( (res)=>{
        console.log(res.data)
        var is_sex =res.data.gender;
        this.setData({nickname:res.data.nickname,attrImg:res.data.avatar,is_sex:is_sex,birthday:res.data.birthday})
  
    }).catch( (errMsg)=>{});
      

  }).catch( (errMsg)=>{});
   

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})